package cn.zwz.customer.controller;

import cn.hutool.core.util.IdUtil;
import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.customer.entity.Customer;
import cn.zwz.customer.service.ICustomerService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 熊英杰
 */
@Slf4j
@RestController
@Api(tags = "客户信息管理接口")
@RequestMapping("/zwz/customer")
@Transactional
public class CustomerController {

    @Autowired
    private ICustomerService iCustomerService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条客户信息")
    public Result<Customer> get(@RequestParam String id){
        return new ResultUtil<Customer>().setData(iCustomerService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部客户信息个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iCustomerService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部客户信息")
    public Result<List<Customer>> getAll(){
        return new ResultUtil<List<Customer>>().setData(iCustomerService.list());
    }

    /**
     * 查询员工信息
     * @param customer 员工
     * @param page 分页参数
     * @return Result<IPage<Customer>>
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询员工信息")
    public Result<IPage<Customer>> getByPage(@ModelAttribute Customer customer ,@ModelAttribute PageVo page){
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(customer.getName())) {
            qw.like("name",customer.getName());
        }
        if(!ZwzNullUtils.isNull(customer.getMobile())) {
            qw.like("mobile",customer.getMobile());
        }
        if(!ZwzNullUtils.isNull(customer.getIdCard())) {
            qw.like("id_card",customer.getIdCard());
        }
        IPage<Customer> data = iCustomerService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Customer>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改客户信息")
    public Result<Customer> saveOrUpdate(Customer customer){
        if(iCustomerService.saveOrUpdate(customer)){
            return new ResultUtil<Customer>().setData(customer);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增客户信息")
    public Result<Customer> insert(Customer customer){
        customer.setCode(IdUtil.getSnowflake(1, 1).nextId() + "");
        iCustomerService.saveOrUpdate(customer);
        return new ResultUtil<Customer>().setData(customer);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑客户信息")
    public Result<Customer> update(Customer customer){
        iCustomerService.saveOrUpdate(customer);
        return new ResultUtil<Customer>().setData(customer);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除客户信息")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iCustomerService.removeById(id);
        }
        return ResultUtil.success();
    }
}
